#include "I2C.H"
#include <REGX52.H>

#define AT24C02_Address 0xA0 

/**
  * @brief   写入一个字节
  * @param   WordAddress要写入的字地址
  * @param   Byte要写入的数据
  * @retval  无
  */
void AT24C02_Write(unsigned char WordAddress,Byte)
{

	 I2C_Start();
	
	 I2C_Send(AT24C02_Address);//器件地址和写命令
	
	 ReceiveAck();//不用判断是否应答，默认应答测试的时候，验证一下就行
	
	 I2C_Send(WordAddress);
	
	 ReceiveAck();
	
	 I2C_Send(Byte);
	
	 ReceiveAck();
	
	 I2C_Stop();

}

/**
  * @brief  读出一个字节的数据
  * @param  WordAddress要读出的字地址
  * @retval 返回读出的数据
  */
unsigned int AT24C02_Read(unsigned char WordAddress)
{
	unsigned char Data = 0;
	
	I2C_Start();	
	
	I2C_Send(AT24C02_Address);
	
	ReceiveAck();
	
	I2C_Send(WordAddress);
	
	ReceiveAck();
	
	I2C_Start();	
	
	I2C_Send(AT24C02_Address | 0x01);
	
	ReceiveAck();
	
	Data = I2C_Receive();
	
	SendAck(1);
	
	I2C_Stop();
	
	return Data;
	
}	










